package com.jqb.ant.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.jqb.ant.dao.UserDao;
import com.jqb.ant.vo.User;

@Repository("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

	@Resource
	private void superSetSessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	
	/* (non-Javadoc)
	 * @see com.jqb.ant.dao.UserDao#addUser()
	 */
	@Override
	public void addUser(User user) {
		this.getHibernateTemplate().save(user);
	}

	/* (non-Javadoc)
	 * @see com.jqb.ant.dao.UserDao#deleteUser()
	 */
	@Override
	public void deleteUser(int id) {
		User user = this.getUserById(id);
		this.getHibernateTemplate().delete(user);
	}

	/* (non-Javadoc)
	 * @see com.jqb.ant.dao.UserDao#updateUser()
	 */
	@Override
	public void updateUser(User user) {
		this.getHibernateTemplate().update(user);
	}

	/* (non-Javadoc)
	 * @see com.jqb.ant.dao.UserDao#getUserById(int)
	 */
	@Override
	public User getUserById(int id) {
		User user =(User) this.getHibernateTemplate().get(User.class, id);
		return user;
	}

	/* (non-Javadoc)
	 * @see com.jqb.ant.dao.UserDao#getUserByName(java.lang.String)
	 */
	@Override
	public User getUserByName(String name) {
		User user = (User) this.getSession().createQuery("from User where name=?").setString(0, name).uniqueResult();
		return user;
	}

	/* (non-Javadoc)
	 * @see com.jqb.ant.dao.UserDao#getUsers()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<User> getUsers() {
		List<User> users = this.getSession().createQuery("from User").list();
		return users;
	}

}
